package cn.com.thingo.shyintelligent.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.th.opensdk.openapi.RemoterInfo;
import com.th.opensdk.openapi.THOpenSDK;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.shyintelligent.R;
import cn.com.thingo.shyintelligent.base.BaseActivity;
import cn.com.thingo.shyintelligent.base.contract.MusicContract;
import cn.com.thingo.shyintelligent.presenter.MusicPresenter;
import cn.com.thingo.shyintelligent.ui.adapter.MusicControlAdapter;
import cn.com.thingo.shyintelligent.ui.adapter.decoration.RecycleViewDivider;

public class MusicActivity extends BaseActivity<MusicPresenter> implements MusicContract.View {
    private THOpenSDK thOpenSDK;
    private List<RemoterInfo> musicList = new ArrayList<>();
    private MusicControlAdapter adapter;
    @BindView(R.id.toolbar_left)
    ImageView ivBack;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    @BindView(R.id.music_control_list)
    RecyclerView rvList;
    private int currentRoomId;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_music;
    }

    @Override
    protected void initEventAndData() {
        thOpenSDK = THOpenSDK.getInstance();
        currentRoomId = getIntent().getIntExtra("current_room_id", -1);
        initView();
        initData();
    }

    private void initData() {
        List<RemoterInfo> remoterList = thOpenSDK.getRemoterList();
        if (remoterList != null && remoterList.size() > 0) {
            for (int i = 0; i < remoterList.size(); i++) {
                int roomId = remoterList.get(i).getRoomId();
                int controlType = remoterList.get(i).getRemoterType();
                if (roomId == currentRoomId && controlType == RemoterInfo.HOME_REMOTER_TYPE_MUSIC) {
                    musicList.add(remoterList.get(i));
                }
            }
        }
        adapter.notifyDataSetChanged();
    }

    private void initView() {
        ivBack.setImageResource(R.mipmap.back);
        String remote_name = getIntent().getStringExtra("remote_name");
        tvTitle.setText(remote_name);
        adapter = new MusicControlAdapter(musicList, mContext);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        rvList.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 2, getResources().getColor(R.color.gray_12)));
        rvList.setAdapter(adapter);

    }

    @OnClick({R.id.toolbar_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
        }
    }

}
